


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: AnnotationPropertyAccessStrategy</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.access</a> ]
</div>

<h1>Coverage Summary for Class: AnnotationPropertyAccessStrategy (at.dormancy.access)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AnnotationPropertyAccessStrategy</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (7/ 7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (32/ 32)
  </span>
</td>
</tr>
  <tr>
    <td class="name">AnnotationPropertyAccessStrategy$1</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (5/ 5)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">AnnotationPropertyAccessStrategy$2</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (8/ 8)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (11/ 11)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (45/ 45)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.access;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.dormancy.persister.filter.MemberFilter;
<i>19</i>&nbsp;import at.dormancy.persister.filter.NonTransientPropertyFilter;
<i>20</i>&nbsp;import org.apache.commons.lang.ArrayUtils;
<i>21</i>&nbsp;
<i>22</i>&nbsp;import javax.annotation.Nonnull;
<i>23</i>&nbsp;import javax.annotation.Nullable;
<i>24</i>&nbsp;import java.beans.PropertyDescriptor;
<i>25</i>&nbsp;import java.lang.annotation.Annotation;
<i>26</i>&nbsp;import java.lang.reflect.AnnotatedElement;
<i>27</i>&nbsp;import java.lang.reflect.Field;
<i>28</i>&nbsp;import java.lang.reflect.Method;
<i>29</i>&nbsp;
<i>30</i>&nbsp;import static at.dormancy.access.AbstractPropertyAccessStrategy.AccessType.*;
<i>31</i>&nbsp;import static org.apache.commons.lang.StringUtils.upperCase;
<i>32</i>&nbsp;import static org.springframework.beans.BeanUtils.findPropertyForMethod;
<i>33</i>&nbsp;import static org.springframework.core.annotation.AnnotationUtils.getValue;
<i>34</i>&nbsp;import static org.springframework.util.ReflectionUtils.*;
<i>35</i>&nbsp;
<i>36</i>&nbsp;/**
<i>37</i>&nbsp; * Determines how to access entity properties based on annotations located on them.
<i>38</i>&nbsp; *
<i>39</i>&nbsp; * @author Gregor Schauer
<i>40</i>&nbsp; * @since 2.0.0
<i>41</i>&nbsp; */
<i>42</i>&nbsp;public abstract class AnnotationPropertyAccessStrategy extends AbstractPropertyAccessStrategy {
<i>43</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<b class="fc"><i>44</i>&nbsp;	protected static Class&lt;? extends Annotation&gt;[] accessAnnotations = ArrayUtils.EMPTY_CLASS_ARRAY;</b>
<i>45</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<b class="fc"><i>46</i>&nbsp;	protected static Class&lt;? extends Annotation&gt;[] idAnnotations = ArrayUtils.EMPTY_CLASS_ARRAY;</b>
<i>47</i>&nbsp;
<b class="fc"><i>48</i>&nbsp;	protected MemberFilter propertyFilter = NonTransientPropertyFilter.getInstance();</b>
<i>49</i>&nbsp;
<i>50</i>&nbsp;	/**
<i>51</i>&nbsp;	 * Creates a new strategy instance.
<i>52</i>&nbsp;	 *
<i>53</i>&nbsp;	 * @param entityType the type of the entity
<i>54</i>&nbsp;	 * @see #initialize(Class)
<i>55</i>&nbsp;	 */
<b class="fc"><i>56</i>&nbsp;	protected AnnotationPropertyAccessStrategy(@Nonnull Class&lt;?&gt; entityType) {</b>
<b class="fc"><i>57</i>&nbsp;		initialize(entityType);</b>
<b class="fc"><i>58</i>&nbsp;	}</b>
<i>59</i>&nbsp;
<i>60</i>&nbsp;	/**
<i>61</i>&nbsp;	 * Initializes this strategy by scanning the desired type for annotations.
<i>62</i>&nbsp;	 *
<i>63</i>&nbsp;	 * @param entityType the entity type
<i>64</i>&nbsp;	 */
<i>65</i>&nbsp;	protected void initialize(@Nonnull Class&lt;?&gt; entityType) {
<b class="fc"><i>66</i>&nbsp;		propertyAccessTypeMap.clear();</b>
<i>67</i>&nbsp;		// Retrieve the default access type for the entity type by looking up access annotations on type level
<b class="fc"><i>68</i>&nbsp;		Annotation accessType = getAnnotation(entityType, getAccessAnnotations());</b>
<b class="fc"><i>69</i>&nbsp;		AccessType entityAccessType = accessType != null ? valueOf(upperCase(String.valueOf(getValue(accessType)))) : null;</b>
<i>70</i>&nbsp;
<i>71</i>&nbsp;		// If the entity type does not define a default access type, scan for methods annotated with an ID annotation
<b class="fc"><i>72</i>&nbsp;		if (entityAccessType == null) {</b>
<b class="fc"><i>73</i>&nbsp;			Method[] methods = getAllDeclaredMethods(entityType);</b>
<b class="fc"><i>74</i>&nbsp;			for (Method method : methods) {</b>
<i>75</i>&nbsp;				// If a method is annotated with a ID annotation, property access is used by default
<b class="fc"><i>76</i>&nbsp;				if (getAnnotation(method, getIdAnnotations()) != null) {</b>
<b class="fc"><i>77</i>&nbsp;					entityAccessType = PROPERTY;</b>
<b class="fc"><i>78</i>&nbsp;					break;</b>
<i>79</i>&nbsp;				}
<i>80</i>&nbsp;			}
<i>81</i>&nbsp;		}
<i>82</i>&nbsp;
<i>83</i>&nbsp;		/**
<i>84</i>&nbsp;		 * If the determination of the default access type was not successful so far i.e., because there is a field
<i>85</i>&nbsp;		 * annotated with the ID annotation, use field access by default
<i>86</i>&nbsp;		 */
<b class="fc"><i>87</i>&nbsp;		if (entityAccessType == null) {</b>
<b class="fc"><i>88</i>&nbsp;			entityAccessType = FIELD;</b>
<i>89</i>&nbsp;		}
<b class="fc"><i>90</i>&nbsp;		setDefaultAccessType(entityAccessType);</b>
<b class="fc"><i>91</i>&nbsp;		final AccessType finalEntityAccessType = entityAccessType;</b>
<i>92</i>&nbsp;
<i>93</i>&nbsp;		// Determine whether some properties must be accessed differently
<b class="fc"><i>94</i>&nbsp;		if (getDefaultAccessType() == FIELD) {</b>
<i>95</i>&nbsp;			// If field access is used by default, scan for fields annotated with an access annotation
<b class="fc"><i>96</i>&nbsp;			doWithFields(entityType, new FieldCallback() {</b>
<i>97</i>&nbsp;				@Override
<i>98</i>&nbsp;				public void doWith(@Nonnull Field field) {
<i>99</i>&nbsp;					// Retrieve the access annotation from the field (if possible)
<b class="fc"><i>100</i>&nbsp;					Annotation annotation = getAnnotation(field, getAccessAnnotations());</b>
<i>101</i>&nbsp;
<i>102</i>&nbsp;					// If a annotation was found, map the access type to the appropriate mode. Otherwise, use the default mode for the type.
<b class="fc"><i>103</i>&nbsp;					AccessType accessType = annotation != null ? valueOf(upperCase(String.valueOf(getValue(annotation)))) : finalEntityAccessType;</b>
<b class="fc"><i>104</i>&nbsp;					propertyAccessTypeMap.put(field.getName(), accessType);</b>
<b class="fc"><i>105</i>&nbsp;				}</b>
<i>106</i>&nbsp;			}, propertyFilter);
<i>107</i>&nbsp;		} else {
<i>108</i>&nbsp;			// If property access is used by default, scan for methods annotated with an access annotation
<b class="fc"><i>109</i>&nbsp;			doWithMethods(entityType, new MethodCallback() {</b>
<i>110</i>&nbsp;				@Override
<i>111</i>&nbsp;				public void doWith(@Nonnull Method method) {
<i>112</i>&nbsp;					// Check whether the given method is an ordinary getter method
<b class="fc"><i>113</i>&nbsp;					PropertyDescriptor descriptor = findPropertyForMethod(method);</b>
<b class="fc"><i>114</i>&nbsp;					if (descriptor == null) {</b>
<b class="fc"><i>115</i>&nbsp;						return;</b>
<i>116</i>&nbsp;					}
<i>117</i>&nbsp;					// Retrieve the access annotation from the method (if possible)
<b class="fc"><i>118</i>&nbsp;					Annotation annotation = getAnnotation(method, getAccessAnnotations());</b>
<i>119</i>&nbsp;
<i>120</i>&nbsp;					// If a annotation was found, map the access type to the appropriate mode. Otherwise, use the default mode for the type.
<b class="fc"><i>121</i>&nbsp;					AccessType accessType = annotation != null ? valueOf(upperCase(String.valueOf(getValue(annotation)))) : finalEntityAccessType;</b>
<b class="fc"><i>122</i>&nbsp;					propertyAccessTypeMap.put(descriptor.getName(), accessType);</b>
<b class="fc"><i>123</i>&nbsp;				}</b>
<i>124</i>&nbsp;			}, propertyFilter);
<i>125</i>&nbsp;		}
<b class="fc"><i>126</i>&nbsp;	}</b>
<i>127</i>&nbsp;
<i>128</i>&nbsp;	/**
<i>129</i>&nbsp;	 * Returns the annotation types used for specifying or overriding the access type.
<i>130</i>&nbsp;	 *
<i>131</i>&nbsp;	 * @return the access annotation type
<i>132</i>&nbsp;	 */
<i>133</i>&nbsp;	@Nonnull
<i>134</i>&nbsp;	public static Class&lt;? extends Annotation&gt;[] getAccessAnnotations() {
<b class="fc"><i>135</i>&nbsp;		return accessAnnotations;</b>
<i>136</i>&nbsp;	}
<i>137</i>&nbsp;
<i>138</i>&nbsp;	/**
<i>139</i>&nbsp;	 * Returns the annotation types used for specifying identifier properties.
<i>140</i>&nbsp;	 *
<i>141</i>&nbsp;	 * @return the identifier annotation types
<i>142</i>&nbsp;	 */
<i>143</i>&nbsp;	@Nonnull
<i>144</i>&nbsp;	public static Class&lt;? extends Annotation&gt;[] getIdAnnotations() {
<b class="fc"><i>145</i>&nbsp;		return idAnnotations;</b>
<i>146</i>&nbsp;	}
<i>147</i>&nbsp;
<i>148</i>&nbsp;	/**
<i>149</i>&nbsp;	 * Finds and returns the first annotation of the desired types present on the given element.
<i>150</i>&nbsp;	 *
<i>151</i>&nbsp;	 * @param element         the annotated element to check
<i>152</i>&nbsp;	 * @param annotationTypes the types of annotations to find
<i>153</i>&nbsp;	 * @return the first annotation of the desired types or {@code null} if the search was not successful
<i>154</i>&nbsp;	 */
<i>155</i>&nbsp;	@Nullable
<i>156</i>&nbsp;	protected Annotation getAnnotation(@Nonnull AnnotatedElement element, @Nonnull Class&lt;? extends Annotation&gt;... annotationTypes) {
<b class="fc"><i>157</i>&nbsp;		for (Class&lt;? extends Annotation&gt; annotationType : annotationTypes) {</b>
<b class="fc"><i>158</i>&nbsp;			Annotation annotation = element.getAnnotation(annotationType);</b>
<b class="fc"><i>159</i>&nbsp;			if (annotation != null) {</b>
<b class="fc"><i>160</i>&nbsp;				return annotation;</b>
<i>161</i>&nbsp;			}
<i>162</i>&nbsp;		}
<b class="fc"><i>163</i>&nbsp;		return null;</b>
<i>164</i>&nbsp;	}
<i>165</i>&nbsp;
<i>166</i>&nbsp;	public void setPropertyFilter(MemberFilter propertyFilter) {
<b class="fc"><i>167</i>&nbsp;		this.propertyFilter = propertyFilter;</b>
<b class="fc"><i>168</i>&nbsp;	}</b>
<i>169</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
